package com.future.direction.common.util;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.future.direction.R;
import com.future.direction.ui.widget.MyPopupWindow;


/**
 * Created by ZHP on 2017/7/4.
 */

public class PopuUtil {


    private View viewLayout;


    /**
     * 在屏幕中间弹出, 确认，取消，提示框
     * @param activity
     * @param rootview
     * @param message
     * @param okClickListener
     * @param cancelClickListener
     */
    public static MyPopupWindow initOkCancelPopupWindow(Activity activity, View rootview, String message, View.OnClickListener okClickListener, View.OnClickListener cancelClickListener) {
        MyPopupWindow mPopupWindow = new PopuUtil().initAtLocationPopuWrap(activity, R.layout.popup_exit, rootview, 265, 120);
        TextView title = mPopupWindow.getContentView().findViewById(R.id.tv_title);
        Button buttonPopupCancel = mPopupWindow.getContentView().findViewById(R.id.button_creat_cancel);
        Button buttonPopupCommitSure = mPopupWindow.getContentView().findViewById(R.id.button_creat_commit_sure);
        title.setText(message);
        buttonPopupCancel.setOnClickListener((View v)->{
            mPopupWindow.dismiss();
            if(cancelClickListener != null){
                cancelClickListener.onClick(v);
            }
        });
        buttonPopupCommitSure.setOnClickListener((View v) ->{
            mPopupWindow.dismiss();
            if(okClickListener != null){
                okClickListener.onClick(v);
            }
        });
        return mPopupWindow;
    }

    /**
     * 在屏幕中间弹出
     *
     * @param activity 所在页面
     * @param layout   popup页面
     * @param view     基于控件
     * @param width    popup宽度
     * @param height   popup高度
     * @return popup
     */
    public MyPopupWindow initAtLocationPopuWrap(Activity activity, int layout, View view, int width, int height) {
        MyPopupWindow myPopupWindow = new MyPopupWindow(activity);
        viewLayout = View.inflate(activity, layout, null);
        myPopupWindow.setWidth(UIUtil.dip2px(width));
        myPopupWindow.setHeight(UIUtil.dip2px(height));
        myPopupWindow.setContentView(viewLayout);
        myPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        myPopupWindow.setOutsideTouchable(true);
        myPopupWindow.setFocusable(true);
        myPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        return myPopupWindow;
    }

    /**
     * 在屏幕底部弹出
     *
     * @param activity 所在页面
     * @param layout   popup页面
     * @param view     基于控件
     * @return popup
     */
    public MyPopupWindow initAtBottomPopuWrap(Activity activity, int layout, View view, int height) {
        MyPopupWindow myPopupWindow = new MyPopupWindow(activity);
        viewLayout = View.inflate(activity, layout, null);
        myPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        myPopupWindow.setHeight(UIUtil.dip2px(height));
        myPopupWindow.setContentView(viewLayout);
        myPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        myPopupWindow.setOutsideTouchable(true);
        myPopupWindow.setFocusable(true);
        myPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        return myPopupWindow;
    }

    /**
     * 弹出 充满宽度
     *
     * @param activity 所在页面
     * @param layout   popup页面
     * @param view     基于控件
     * @param gravity  所在位置
     * @param x        X轴
     * @param y        Y轴
     * @return popup
     */
    public MyPopupWindow initAtLocationPopu(final Activity activity, int layout, View view, int gravity, int x, int y) {
        MyPopupWindow myPopupWindow = new MyPopupWindow(activity);
        myPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        myPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        viewLayout = View.inflate(activity, layout, null);
        myPopupWindow.setContentView(viewLayout);
        myPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        myPopupWindow.setOutsideTouchable(true);
        myPopupWindow.setFocusable(true);
        myPopupWindow.showAtLocation(view, gravity, x, y);
        return myPopupWindow;
    }
    /*public MyPopupWindow initAnmAtLocationPopu(final Activity activity, int layout, View view, int gravity, int x, int y) {
        MyPopupWindow myPopupWindow = new MyPopupWindow(activity);
        myPopupWindow.setAnimationStyle(R.style.popwiny_anim_style);
        myPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        myPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        viewLayout = View.inflate(activity, layout, null);
        myPopupWindow.setContentView(viewLayout);
        myPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        myPopupWindow.setOutsideTouchable(true);
        myPopupWindow.setFocusable(true);
        myPopupWindow.showAtLocation(view,gravity,x,y);
        return myPopupWindow;
    }*/

    /**
     * 在屏幕中间弹出 充满宽度 高度
     *
     * @param activity 所在页面
     * @param layout   popup页面
     * @param view     基于控件
     * @param gravity  所在位置
     * @param x        X轴
     * @param y        Y轴
     * @return popup
     */
    public MyPopupWindow initAtLocationPopuMat(final Activity activity, int layout, View view, int gravity, int x, int y) {
        MyPopupWindow myPopupWindow = new MyPopupWindow(activity);
        myPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        myPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        viewLayout = View.inflate(activity, layout, null);
        myPopupWindow.setContentView(viewLayout);
        myPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        myPopupWindow.setOutsideTouchable(true);
        myPopupWindow.setFocusable(true);
        myPopupWindow.showAtLocation(view, gravity, x, y);
        return myPopupWindow;
    }

    /**
     * 在控件下方弹出
     *
     * @param activity 所在页面
     * @param layout   popup页面
     * @param view     基于控件
     * @return popup
     */
    public MyPopupWindow initDropDownPopu(Activity activity, int layout, View view) {
        viewLayout = View.inflate(activity, layout, null);
        MyPopupWindow myPopupWindow = new MyPopupWindow(activity);
        myPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        myPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        myPopupWindow.setContentView(viewLayout);
        myPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        myPopupWindow.setOutsideTouchable(true);
        myPopupWindow.setFocusable(true);
        myPopupWindow.showAsDropDown(view);
        return myPopupWindow;
    }


    /**
     * 在控件正上方弹出
     *
     * @param activity 所在页面
     * @param layout   popup页面
     * @param view     基于控件
     * @param gravity  弹出位置
     * @return popup
     */
    public MyPopupWindow initDropUpPopu(Activity activity, int layout, View view, int gravity) {
        viewLayout = View.inflate(activity, layout, null);
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        MyPopupWindow myPopupWindow = new MyPopupWindow(activity);
        myPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        myPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        myPopupWindow.setContentView(viewLayout);
        myPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        myPopupWindow.setOutsideTouchable(true);
        myPopupWindow.setFocusable(true);
        viewLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int measuredWidth = myPopupWindow.getContentView().getMeasuredWidth();
        int measuredHeight = myPopupWindow.getContentView().getMeasuredHeight();
        myPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY,
                (location[0] + view.getWidth() / 2) - measuredWidth / 2, location[1] - measuredHeight);
        return myPopupWindow;
    }

    /**
     * 在屏幕中间上半部分弹出
     *
     * @param activity 所在页面
     * @param layout   popup页面
     * @param view     基于控件
     * @param width    popup宽度
     * @param height   popup高度
     * @return popup
     */
    public MyPopupWindow initAtLocationPopuWrapUp(Activity activity, int layout, View view, int width, int height) {
        MyPopupWindow myPopupWindow = new MyPopupWindow(activity);
        viewLayout = View.inflate(activity, layout, null);
        viewLayout.measure(makeDropDownMeasureSpec(myPopupWindow.getWidth()),
                makeDropDownMeasureSpec(myPopupWindow.getHeight()));
        myPopupWindow.setWidth(UIUtil.dip2px(width));
        myPopupWindow.setHeight(UIUtil.dip2px(height));
        myPopupWindow.setContentView(viewLayout);
        myPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        myPopupWindow.setOutsideTouchable(true);
        myPopupWindow.setFocusable(true);

        int offsetY = -myPopupWindow.getContentView().getMeasuredHeight();

        myPopupWindow.showAtLocation(view, Gravity.CENTER, 0, offsetY);
        return myPopupWindow;
    }

    @SuppressWarnings("ResourceType")
    private static int makeDropDownMeasureSpec(int measureSpec) {
        int mode;
        if (measureSpec == ViewGroup.LayoutParams.WRAP_CONTENT) {
            mode = View.MeasureSpec.UNSPECIFIED;
        } else {
            mode = View.MeasureSpec.EXACTLY;
        }
        return View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.getSize(measureSpec), mode);
    }

    public View getViewLayout() {
        return viewLayout;
    }
}
